<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Component
 * @version 		$Id: profile.class.php 4208 2012-06-04 14:37:39Z Miguel_Espinoza $
 */
class User_Component_Controller_Updateprofile extends Phpfox_Component
{
	/**
	 * Class process method wnich is used to execute this component.
	 */
	public function process()
	{
		Phpfox::isUser(true);
            $aImage = Phpfox::getLib('file')->load('image', array('jpg', 'gif', 'png'), (Phpfox::getUserParam('user.max_upload_size_profile_photo') === 0 ? null : (Phpfox::getUserParam('user.max_upload_size_profile_photo') / 1024)));
//			die('erwerwerawer');
		if ($aVals = $this->request()->get('custom'))
		{
                    
			$aCustomFields = Phpfox::getService('custom')->getForEdit(array('user_main', 'user_panel', 'profile_panel'), Phpfox::getUserId(), Phpfox::getUserBy('user_group_id'), false, Phpfox::getUserId());
                        
			foreach ($aCustomFields as $aCustomField)
			{				
				if (
					(empty($aVals[$aCustomField['field_id']]) && $aCustomField['is_required']) 
					)
				{
					Phpfox_Error::set(Phpfox::getPhrase('user.the_field_field_is_required', array('field' => Phpfox::getPhrase($aCustomField['phrase_var_name']))));
				}
				else if ((!isset($aVals[$aCustomField['field_id']]) || empty($aVals[$aCustomField['field_id']])) && !$aCustomField['is_required'])
				{
					Phpfox::getService('custom.process')->updateField($aCustomField, Phpfox::getUserId(), Phpfox::getUserId(),'');
				}
			}			
			
			if (Phpfox_Error::isPassed())
			{
				$bReturnCustom = Phpfox::getService('custom.process')->updateFields(Phpfox::getUserId(), Phpfox::getUserId(), $aVals);
                                
				$aUser = $this->request()->get('val');
				$aUser['language_id'] = Phpfox::getUserBy('language_id');
				define('PHPFOX_IS_CUSTOM_FIELD_UPDATE', true);
                                //upload image
                                if(isset($aImage)){
                                    $aImage = Phpfox::getService('user.process')->uploadImage(Phpfox::getUserId(), true);
                                }
                                
				$bReturnUser = Phpfox::getService('user.process')->update(Phpfox::getUserId(), $aUser);
				if ($bReturnCustom && $bReturnUser)
				{
					echo ('<script> alert("dfsdfs");$(\'#js_custom_submit_button\').attr(\'disabled\', false).removeClass(\'disabled\'); $(\'#js_custom_update_info\').html(\''.str_replace("'", "\\'", Phpfox::getPhrase('user.done')).'\').fadeOut(5000);</script>');
//					->slideDown('#js_custom_public_message');
					return true;
				}
			}
			
			echo ('<script>$(\'#js_custom_submit_button\').attr(\'disabled\', false).removeClass(\'disabled\'); $(\'#js_custom_update_info\').hide();</script>');			
			
		}
                
	}
	
	/**
	 * Garbage collector. Is executed after this class has completed
	 * its job and the template has also been displayed.
	 */
	public function clean()
	{
		(($sPlugin = Phpfox_Plugin::get('user.component_controller_updateprofile_clean')) ? eval($sPlugin) : false);
	}
}

?>
